home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-08-12 | 1.6 KB | 60 lines | [TEXT/NJML] |
- (*
- System.Directory.cd "../tools/prof/";
- use "e_profile_temp";
- *)
-
- signature SORT =
- sig
- (* pass the gt predicate as an argument *)
- val sort : ('a * 'a -> bool) -> 'a list -> 'a list
- val sorted : ('a * 'a -> bool) -> 'a list -> bool
- val makelist : int * int list -> int list
- end
-
- structure Sort : SORT = struct
-
- (* smooth applicative merge sort
- * Taken from, ML for the Working Programmer, LCPaulson. pg 99-100
- *)
- fun sort (op > : 'a * 'a -> bool) ls =
- let fun merge([],ys) = ys
- | merge(xs,[]) = xs
- | merge(x::xs,y::ys) =
- if x > y then y::merge(x::xs,ys) else x::merge(xs,y::ys)
- fun mergepairs(ls as [l], k) = ls
- | mergepairs(l1::l2::ls,k) =
- if k mod 2 = 1 then l1::l2::ls
- else mergepairs(merge(l1,l2)::ls, k div 2)
- fun nextrun(run,[]) = (rev run,[])
- | nextrun(run,x::xs) = if x > hd run then nextrun(x::run,xs)
- else (rev run,x::xs)
- fun samsorting([], ls, k) = hd(mergepairs(ls,0))
- | samsorting(x::xs, ls, k) =
- let val (run,tail) = nextrun([x],xs)
- in samsorting(tail, mergepairs(run::ls,k+1), k+1)
- end
- in case ls of [] => [] | _ => samsorting(ls, [], 0)
- end
-
- fun sorted (op >) =
- let fun s (x::(rest as (y::_))) = not(x>y) andalso s rest
- | s l = true
- in s
- end
-
- fun makelist (i,ls) =
- let fun m (0,ls) = ls
- | m (i,ls) = m(i-1,i::ls)
- in m(i,ls)
- end
-
- end;
-
- !Profile.profiling;
-
- Profile.profileOn();
- Sort.sort (op <) (Sort.makelist (20000,nil));
- Profile.profileOff();
-
- Profile.report std_out;
-